<template>
  <!-- 部门管理 -->
  <div class="box">
    <avue-crud :ref="refName"
               :option="option"
               :page="page"
               :table-loading="tableLoading"
               @row-update="handleUpdate"
               @row-save="handleSave"
               @row-del="rowDel"
               @size-change="sizeChange"
               @current-change="currentChange"
               @refresh-change="refreshChange"
               @search-reset="searchChange"
               @search-change="searchChange"
               :before-open="beforeOpen"
               v-model="form"
               :data="tableData">
      <template #menu="{ size, row, index }">
        <el-button v-if="buttonPermissionsCheck('SYS_DEPT_ADD')"
                   @click="handleAdd(row)"
                   text type="primary"
                   :size="size">添加
        </el-button>
      </template>
      <template #menu-left>

        <el-button v-if="buttonPermissionsCheck('SYS_DEPT_ADD')"
                   type="primary"
                   @click="handleAdd(null)">新增
        </el-button>
      </template>
    </avue-crud>
  </div>
</template>

<script setup>
import {option} from '@/table/sys/dept/list.js'

const httpService = inject("httpService")

const {
  form,
  page,
  refName,
  searchForm,
  tableData,
  tableLoading,
  handleSave,
  handleUpdate,
  rowDel,
  searchChange,
  refreshChange,
  sizeChange,
  currentChange,
  activated,
  url,
  buttonPermissionsCheck
} = useTableMixins(option, "sys-dept", {
  listUrl: "findTreeAuth",
  getList: (page) => {
    tableLoading.value = true;
    let query = Object.assign({}, searchForm.value, page);
    httpService.get("/" + url.value + "/findTreeAuth", query).then(res => {
      tableData.value = res;
      tableLoading.value = false;
    }).catch(() => {
      tableLoading.value = false;
    })
  }
})


//定义组件ref
const crud = ref(null);

//定义当前选中
const obj = ref({});
/**
 * 自定义添加
 */
const handleAdd = (row) => {
  obj.value=row;
  if (crud.value) {
    crud.value.rowAdd();
  } else {
    ElMessage.error('CRUD组件未加载');
  }
}

/**
 * 弹框打开前的回调
 * @param done
 * @param type
 */
const beforeOpen = (done, type) => {
  if (type === 'add' && obj.value != null) {
    form.value.parentId = obj.value.deptId
  }
  if( form.value.parentId==='0'){
    form.value.parentId=null;
  }
  done()
}

onActivated(() => {
  activated()
})
</script>

<style scoped>

</style>
